package com.xuecheng.manage_media.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.xuecheng.framework.domain.media.MediaFile;
import com.xuecheng.framework.domain.media.request.QueryMediaFileRequest;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.manage_media.dao.MediaFileRepository;

@Service
public class MediaFileService {
    
    @Autowired
    MediaFileRepository mediaFileRepository;

    //查询我的媒资列表
    public QueryResponseResult<MediaFile> findList(int page, int size, 
            QueryMediaFileRequest queryMediaFileRequest) {
        if(queryMediaFileRequest == null) {
            queryMediaFileRequest = new QueryMediaFileRequest();
        }
        //条件值对象
        MediaFile mediaFile = new MediaFile();
        if(StringUtils.isNotEmpty(queryMediaFileRequest.getTag())) {
            mediaFile.setTag(queryMediaFileRequest.getTag());
        }
        if(StringUtils.isNotEmpty(queryMediaFileRequest.getFileOriginalName())) {
            mediaFile.setFileOriginalName(queryMediaFileRequest.getFileOriginalName());
        }
        if(StringUtils.isNotEmpty(queryMediaFileRequest.getProcessStatus())) {
            mediaFile.setProcessStatus(queryMediaFileRequest.getProcessStatus());
        }
        //定义条件匹配器
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                 .withMatcher("tag", ExampleMatcher.GenericPropertyMatchers.contains())//模糊匹配
                 .withMatcher("fileOriginalName", ExampleMatcher.GenericPropertyMatchers.contains())
                 .withMatcher("processStatus", ExampleMatcher.GenericPropertyMatchers.exact());//默认是精确匹配，可以不设置
        //定义Example对象
        Example<MediaFile> example = Example.of(mediaFile,exampleMatcher);
        if(page<=0) {
            page = 1;
        }
        page = page - 1;
        if(size<=0) {
            size = 10;
        }
        //分页查询对象
        Pageable pageable = new PageRequest(page, size);
        //分页查询
        Page<MediaFile> pageAll = mediaFileRepository.findAll(example,pageable);
        //获取总记录数
        long total = pageAll.getTotalElements();
        //获取数据列表
        List<MediaFile> content = pageAll.getContent();
        //返回的数据封装对象
        QueryResult<MediaFile> queryResult = new QueryResult<>();
        queryResult.setTotal(total);
        queryResult.setList(content);
        return new QueryResponseResult<MediaFile>(CommonCode.SUCCESS, queryResult);
    }

}
